package com.growthsee.rider.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.growthsee.common.core.controller.BaseController;
import com.growthsee.common.core.domain.AjaxResult;
import com.growthsee.common.core.page.PageDomain;
import com.growthsee.common.core.page.TableDataInfo;
import com.growthsee.common.utils.SecurityUtils;
import com.growthsee.common.utils.bean.BeanUtils;
import com.growthsee.rider.domain.RiderCourse;
import com.growthsee.rider.domain.RiderCourseExtend;
import com.growthsee.rider.domain.dto.RiderCourseDto;
import com.growthsee.rider.enumeration.StatusEnum;
import com.growthsee.rider.enumeration.YesNoEnum;
import com.growthsee.rider.param.BaseParam;
import com.growthsee.rider.param.CourseParam;
import com.growthsee.rider.service.RiderCourseExtendService;
import com.growthsee.rider.service.RiderCourseService;
import com.growthsee.rider.utils.BeanUtilsExt;
import com.growthsee.rider.wrapper.CourseWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.val;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author csy
 * @since 2021-05-13
 */
@RestController
@RequestMapping("/rider/course")
@AllArgsConstructor
@Api("课程信息")
public class RiderCourseController extends BaseController {
    private final RiderCourseService riderCourseService;
    private final RiderCourseExtendService riderCourseExtendService;


    @ApiOperation(value = "条件查询课程", notes = "条件查询课程")
    @GetMapping("/param")
    public AjaxResult param(CourseParam courseParam) {
        LambdaQueryWrapper<RiderCourse> lambdaQuery = Wrappers.lambdaQuery();
        if(!Objects.isNull(courseParam.getId())){
            lambdaQuery.eq(RiderCourse::getId,courseParam.getId());
        }
        if(!Objects.isNull(courseParam.getType())){
            lambdaQuery.eq(RiderCourse::getType,courseParam.getType());
        }
        if(StringUtils.isNotEmpty(courseParam.getCategoryId())){
            lambdaQuery.eq(RiderCourse::getCategoryId,courseParam.getCategoryId());
        }
        List<RiderCourse> list = riderCourseService.list(lambdaQuery);
        return AjaxResult.success(list);
    }
    @ApiOperation(value = "设置通关题数", notes = "设置通关题数")
    @PutMapping("/passScore")
    public AjaxResult setPassScore(@RequestBody RiderCourse riderCourse) {
        riderCourseService.update(Wrappers.<RiderCourse>lambdaUpdate()
                .set(RiderCourse::getPassScore,riderCourse.getPassScore())
                .eq(RiderCourse::getId,riderCourse.getId()));
        return AjaxResult.success("设置成功");
    }


    @GetMapping
    public TableDataInfo query(PageDomain page, BaseParam baseParam) {
        LambdaQueryWrapper<RiderCourse> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(RiderCourse::getDeleted, YesNoEnum.NO.getValue());
        if(StringUtils.isNotEmpty(baseParam.getKeywords())){
            queryWrapper.like(RiderCourse::getName, baseParam.getKeywords());
        }
        if(!Objects.isNull(baseParam.getType())){
            queryWrapper.like(RiderCourse::getType, baseParam.getType());
        }

        Page page1 = riderCourseService.page(new Page<>(page.getPageNum(),page.getPageSize()), queryWrapper);
        val dtoPage = BeanUtilsExt.copyPageData(page1,RiderCourseDto.class);
        CourseWrapper.builder().dataHandler(dtoPage.getRecords());
        return getDataTable(dtoPage.getRecords(),dtoPage.getTotal());
    }

    @PostMapping
    public AjaxResult insert(@RequestBody RiderCourseDto riderCourse) {
        String checkResult = CourseWrapper.builder().check(riderCourse);
        CourseWrapper.builder().emoji2Str(riderCourse);
        if(StringUtils.isNotEmpty(checkResult))return AjaxResult.error(checkResult);
        riderCourse.setCreateBy(SecurityUtils.getUsername());
        riderCourse.setCreateTime(new Date());
        riderCourse.setDeleted(YesNoEnum.NO.getValue());
        riderCourse.setStatus(StatusEnum.ENABLE.getValue());
        riderCourseService.save(riderCourse);
        RiderCourseExtend courseExtend = riderCourse.getCourseExtend();
        courseExtend.setId(riderCourse.getId());
        riderCourseExtendService.save(courseExtend);
        CourseWrapper.builder().riderAttachmentHandler(riderCourse);
        return AjaxResult.success();
    }

    @PutMapping
    public AjaxResult update(@RequestBody RiderCourseDto riderCourse) {
        String checkResult = CourseWrapper.builder().check(riderCourse);
        if(StringUtils.isNotEmpty(checkResult))return AjaxResult.error(checkResult);
        CourseWrapper.builder().emoji2Str(riderCourse);
        riderCourse.setUpdateBy(SecurityUtils.getUsername());
        riderCourse.setUpdateTime(new Date());
        riderCourseService.updateById(riderCourse);
        riderCourseExtendService.update(Wrappers.<RiderCourseExtend>lambdaUpdate()
                .set(RiderCourseExtend::getContent,riderCourse.getCourseExtend().getContent())
                .eq(RiderCourseExtend::getId,riderCourse.getId())
        );
        CourseWrapper.builder().riderAttachmentHandler(riderCourse);
        return AjaxResult.success();
    }


    @GetMapping("{id}")
    @ApiOperation(value = "根据id查询题型", notes = "根据id查询题型")
    public AjaxResult findOne(@PathVariable String id) {
        RiderCourse riderCourse = riderCourseService.getById(id);
        RiderCourseDto riderCourseDto =new RiderCourseDto();
        BeanUtils.copyBeanProp(riderCourseDto,riderCourse);
        RiderCourseExtend extend = riderCourseExtendService.getById(id);
        riderCourseDto.setCourseExtend(extend);
        CourseWrapper.builder().str2emoji(riderCourseDto);
        CourseWrapper.builder().dataHandler(Arrays.asList(riderCourseDto));
        CourseWrapper.builder().huildQuestion(riderCourseDto);
        return AjaxResult.success(riderCourseDto);
    }

    @DeleteMapping("{id}")
    @ApiOperation(value = "根据id查询题型", notes = "根据id查询题型")
    public AjaxResult deleteById(@PathVariable String id) {
        riderCourseService.update(Wrappers.<RiderCourse>lambdaUpdate()
                .set(RiderCourse::getDeleted,YesNoEnum.YES.getValue())
                .eq(RiderCourse::getId,id));
        return AjaxResult.success();
    }


    @GetMapping("/course/exam")
    @ApiOperation(value = "", notes = "获取课程试题")
    public AjaxResult getEntryExam(CourseParam courseParam) {
        LambdaQueryWrapper<RiderCourse> queryWrapper = Wrappers.<RiderCourse>lambdaQuery().eq(RiderCourse::getDeleted, YesNoEnum.NO.getValue());
        if(!Objects.isNull(courseParam.getType())){
            queryWrapper.eq(RiderCourse::getType, courseParam.getType());
        }
        if(!Objects.isNull(courseParam.getCategoryId())){
            queryWrapper.eq(RiderCourse::getCategoryId, courseParam.getCategoryId());
        }
        if(!Objects.isNull(courseParam.getId())){
            queryWrapper.eq(RiderCourse::getId, courseParam.getId());
        }
        RiderCourse riderCourse = riderCourseService.getOne(queryWrapper);
        if(Objects.isNull(riderCourse))return AjaxResult.error("课程不存在");
        RiderCourseDto riderCourseDto = new RiderCourseDto();
        BeanUtils.copyBeanProp(riderCourseDto,riderCourse);
        String reseult=CourseWrapper.builder().huildQuestion(riderCourseDto);
        if(StringUtils.isNotEmpty(reseult))return AjaxResult.error(reseult);
        RiderCourseExtend extend = riderCourseExtendService.getById(riderCourse.getId());
        riderCourseDto.setCourseExtend(extend);
        CourseWrapper.builder().str2emoji(riderCourseDto);
        return AjaxResult.success(riderCourseDto);
    }
}

